<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <img alt="logo" class="logo" src="@/assets/img/logo.png" />
        <span class="title">蓝景项目后台管理</span>
      </div>
      <div class="desc">以代码敲击梦想</div>
    </div>
    <div class="login">
      <a-form @submit="onSubmit" :form="form">
        <a-tabs
          size="large"
          :tabBarStyle="{ textAlign: 'center' }"
          style="padding: 0 2px;"
        >
          <a-tab-pane tab="账户密码登录" key="1">
            <a-alert
              type="error"
              :closable="true"
              v-show="error"
              :message="error"
              showIcon
              style="margin-bottom: 24px;"
            />
            <a-form-item>
              <a-input
                autocomplete="autocomplete"
                size="large"
                placeholder="请输入手机号码"
                v-decorator="[
                  'phone',
                  {
                    rules: [
                      { required: true, message: '请输入手机号码' },
                      {
                        pattern: /^1[3456789]\d{9}$/,
                        message: '手机号码格式不正确',
                      },
                    ],
                  },
                ]"
              >
                <a-icon slot="prefix" type="user" />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-input
                size="large"
                placeholder="请输入密码"
                autocomplete="autocomplete"
                type="password"
                v-decorator="[
                  'password',
                  {
                    rules: [
                      { required: true, message: '请输入密码' },
                      {
                        pattern: /^[a-zA-Z0-9]{4,16}$/g,
                        message: '密码必须为4-16位的字母数字组合',
                      },
                    ],
                  },
                ]"
              >
                <a-icon slot="prefix" type="lock" />
              </a-input>
            </a-form-item>
          </a-tab-pane>
        </a-tabs>
        <a-form-item>
          <a-button
            :loading="logging"
            style="width: 100%;margin-top: 24px"
            size="large"
            html-type="submit"
            type="primary"
            >登录</a-button
          >
        </a-form-item>
        <div class="register">
          <!-- <a style="float: right;margin-left: 10px">忘记密码</a> -->
          <router-link style="float: right" to="/register"
            >注册账户</router-link
          >
        </div>
      </a-form>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from '@/layouts/CommonLayout';
// import { login, getRoutesConfig } from '@/services/user'
import { login } from '@/services/user';
// import { projectsList } from '@/services/project';
import { setAuthorization } from '@/utils/request';
// import { loadRoutes } from '@/utils/routerUtil'
import { mapMutations } from 'vuex';

export default {
  name: 'Login',
  components: { CommonLayout },
  beforeCreate() {
    this.form = this.$form.createForm(this);
  },
  data() {
    return {
      logging: false,
      error: '',
    };
  },
  computed: {
    systemName() {
      return this.$store.state.setting.systemName;
    },
    systemLogo() {
      return this.$store.state.setting.systemLogo;
    },
  },
  methods: {
    ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles']),
    ...mapMutations('setting', ['setSystemName', 'setSystemLogo']),
    onSubmit(e) {
      e.preventDefault();
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true;
          const name = this.form.getFieldValue('phone');
          const password = this.form.getFieldValue('password');
          login(name, password)
            .then(this.afterLogin)
            .catch((err) => {
              this.logging = false;
              this.$message.error(err.msg);
            });
        }
      });
    },
    async afterLogin(res) {
      this.logging = false;
      const loginRes = res.result;

      if (res.msg === '登录成功') {
        // const { name, permissions, type } = loginRes.result
        const { name, type } = loginRes;
        this.setUser(loginRes);
        // this.setPermissions(permissions)
        let role = {};
        if (type == 3) {
          role.id = 'admin';
          localStorage.setItem('adminInfo', JSON.stringify(loginRes));
        } else {
          role.id = 'user';
        }
        let rolesArr = [];
        rolesArr.push(role);
        this.$store.commit('account/setRoles', rolesArr);
        setAuthorization({
          token: loginRes.token,
          expireAt: new Date(loginRes.token_out_of_date),
        });

        this.$router.push('/user/admininfo');
        this.$message.success('欢迎回来,' + name);
        // 获取路由配置
        // getRoutesConfig().then(result => {
        //   const routesConfig = result.data.data
        //   loadRoutes(routesConfig)
        //   this.$router.push('/dashboard/workplace')
        //   this.$message.success(loginRes.msg, 3)
        // })
      } else {
        this.error = loginRes.msg;
      }
    },

    // async getProject() {
    //   const res = await projectsList();
    //   return res;
    // },
  },
};
</script>

<style lang="less" scoped>
.common-layout {
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        width: 44px;
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 33px;
        color: @title-color;
        font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica,
          sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }
    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }
  .login {
    width: 368px;
    margin: 0 auto;
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;
      &:hover {
        color: @primary-color;
      }
    }
  }
}
</style>
